<template>
    <rx-dialog @cancel="closeWindow" :showok="false" order="buttom" btnalign="center">
            <rx-fit :isheader="isheader">
                <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
                    <div class="table-operator">
                        <a-button @click="accessAdd">{{i18n('management.addBtn','新增')}}</a-button>
                        <a-button type="primary" @click="accessAddGroup">{{i18n('management.addGroupBtn','新增组')}}</a-button>
                        <a-button @click="accessDel">{{i18n('management.delBtn','删除')}}</a-button>
                    </div>
                </div>
                <rx-grid
                    ref="table"
                    :allowRowSelect="true"
                    :multiSelect="true"
                    :columns="columns"
                    :defaultPageSize="10"
                    :url="'/api-bpm/bpm/core/bpmInstPermission/queryPermission?instId='+instId"
                    :queryParam="queryParam"
                    data-field="result.data"
                    :pageSizeOptions="[10,30,40]"
                    id-field="id"
                    @selectChange="onSelectChange"
                    :handRowClass="handRowClass"
                >
                    <template slot="type" slot-scope="{text, record, index}">
                        {{typeMap[text]}}
                    </template>
                    <template slot="status" slot-scope="{text, record, index}">
                        {{text=='YES'?i18n('management.valid','有效'):i18n('management.invalid','无效')}}
                    </template>
                    <template slot="updateTime" slot-scope="{text, record, index}">
                        {{record.status=='NO'?record.updateTime : ""}}
                    </template>
                </rx-grid>
            </rx-fit>
    </rx-dialog>
</template>

<script>
    import {BaseList, RxDialog, RxFit, Util} from '@lowcode/jpaas-common-lib';
    import accessAdd from "./BpmInstPermissionAdd";
    import accessAddGroup from "./BpmInstPermissionAddGroup";
    import accessDel from "./BpmInstPermissionDel";
    import {debounce} from "lodash-es";

    export default {
        name: "BpmInstPermissionManagement",
        components: {
            RxFit,
            RxDialog
        },
        mixins:[BaseList],
        props: {
            layerid: String,
            destroy: Function,
            instId: {
                type: String,
                required: true
            }
        },
        data(){
            return {
                columns: [
                    {title: this.i18n('management.list.index','序号'), type: 'indexColumn', width: 100, dataIndex: 'serial', scopedSlots: {customRender: 'serial'}},
                    {title: this.i18n('management.list.createTime','授权时间'), dataIndex: 'createTime', width: 100},
                    {title: this.i18n('management.list.type','权限类型'), dataIndex: 'type', width: 100, scopedSlots: {customRender: 'type'}},
                    {title: this.i18n('management.list.authName','授权用户'), dataIndex: 'authName', width: 100},
                    {title: this.i18n('management.list.status','状态'), dataIndex: 'status', width: 100, scopedSlots: {customRender: 'status'}},
                    {title: this.i18n('management.list.createByName','添加人'), dataIndex: 'createByName', width: 100},
                    {title: this.i18n('management.list.updateTime','失效时间'), dataIndex: 'updateTime', width: 100,scopedSlots: {customRender: 'updateTime'}},
                    {title: this.i18n('management.list.updateByName','删除操作人'), dataIndex: 'updateByName', width: 100},
                    {title: this.i18n('management.list.desc','备注'), dataIndex: 'desc', width: 100}
                ],
                typeMap:{
                    'handler':this.i18n('management.handler','经办人'),
                    'admin':this.i18n('management.admin','流程管理员'),
                    'role':this.i18n('management.role','流程角色'),
                    'group':this.i18n('management.group','授权组')
                }
            }
        },
        created() {

        },
        methods:{
            i18n(name,text,key){
                return this.$ti18(name,text,"BpmInstPermissionManagement",key);
            },
            closeWindow() {
                Util.closeWindow(this, 'cancel')
            },
            handRowClass(record,index){
                if(record.status=='NO'){
                    return 'simpleGrayRow';
                }
                return '';
            },
            accessAdd() {
                var self_ = this;
                Util.open({
                    component: accessAdd,
                    curVm: this,
                    title: this.i18n("management.addPermission","新增查阅用户"),
                    data: {instId:this.instId},
                    widthHeight: ['800px', '400px']
                }, debounce(function (action, data) {
                    if (action != 'ok') return;

                    self_.refreshGrid();
                },600))
            },
            accessAddGroup() {
                var self_ = this;
                Util.open({
                    component: accessAddGroup,
                    curVm: this,
                    title: this.i18n("management.addPermissionGroup","新增查阅组"),
                    data: {instId:this.instId},
                    widthHeight: ['800px', '400px']
                }, debounce(function (action, data) {
                    if (action != 'ok') return;

                    self_.refreshGrid();
                },600))
            },
            accessDel(){
                var row=this.$refs.table.getSelectedRow();
                if(row){
                    if(row.status=='NO'){
                        this.$message.error(this.i18n("management.msg.validRow","请选择有效的记录"));
                        return;
                    }
                    if(row.type!='handler'){
                        this.$message.error(this.i18n("management.msg.validDelRow","只允许删除流程经办人权限，其他类型权限不能删除。"));
                        return;
                    }
                    var self_=this;
                    Util.open({
                        component:accessDel,
                        curVm:this,
                        title:this.i18n("management.delPermission","删除阅读权限"),
                        data:{id:row.id,authId:row.authId,createTime:row.createTime,instId:this.instId},
                        widthHeight: ['800px', '400px']
                    },debounce(function (action,data){
                        if(action!='ok')return;
                        self_.refreshGrid();
                    },600))
                }else{
                    this.$message.error(this.i18n("management.msg.validEmptyRow","请先选择一个记录！！！"));
                }
            }
        }
    }
</script>

<style scoped>

</style>